package org.wys;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public final class HibernateUtil {

	private static Configuration cf;
	private static SessionFactory sf;
	
	private HibernateUtil(){
		
	}
	
	static{
		cf=new Configuration();
		sf=cf.configure().buildSessionFactory();
	}
	
	public static SessionFactory getSf() {
		return sf;
	}

	public static void Add(Object ob){
		Session se=null;
		Transaction tx=null;
		try{
			se=getSf().openSession();
			tx=se.beginTransaction();
			se.save(ob);
			tx.commit();
		}finally{
			if(se!=null){
				se.close();
			}
		}		
	}
	
	public static void Add(List<Object> obs){
		Session se=null;
		Transaction tx=null;
		try{
			se=getSf().openSession();
			tx=se.beginTransaction();
			for(Object ob:obs){
				se.save(ob);
			}			
			tx.commit();
		}finally{
			if(se!=null){
				se.close();
			}
		}		
	}
}
